<template>
  <div>
    <!-- 操作 -->
    <el-row class="setting-yly-row">
      <el-col :span="14" style="text-align:left;padding-left:15px" class="setting-yly-row-col">
        <el-input v-model="searchName" placeholder="请输入名称" size="small" style="max-width:240px" @keydown.enter="search">
          <el-button slot="suffix" type="text" size="small" class="search-button" @click="search">
            <i class="el-icon-search"></i>
          </el-button>
        </el-input>
        <el-button type="text" size="small" style="margin-left: 20px;" @click="add">
          添加拼团商品
        </el-button>
      </el-col>
    </el-row>

    <!-- 表格 -->
    <div>
      <el-table :data="goodsList" style="width:100%" stripe>
        <el-table-column prop="id" label="商品ID" align="center"></el-table-column>
        <el-table-column prop="name" label="商品名称" align="center"></el-table-column>
        <el-table-column prop="format_create_time" label="时间" align="center"></el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="scope" width="80px">
            <el-switch v-model="scope.row.is_open_assemble" active-color="#13ce66" active-value="1" inactive-value="0" @change="changeStatus(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button class="action-button" type="text"
                       @click="edit(scope.row)"><i class="el-icon-edit"/></el-button>
            <!--<el-button class="action-button"-->
                       <!--@click="del(scope.row)" type="text">-->
              <!--<svg-icon icon-class="shanchu"/>-->
            <!--</el-button>-->
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div style="text-align:right;margin:15px 15px">
      <el-pagination class="page"
        background
        layout="total, prev, pager, next, jumper"
        :total="count"
        :page-size="10"
        :current-page="page"
        @size-change="changePage"
        @current-change="changePage">
      </el-pagination>
    </div>

    <el-dialog :visible.sync="openAdd" :title="title" width="600px" center>
      <el-row class="setting-yly-row">
        <el-col :span="14" style="text-align:left;padding-left:15px" class="setting-yly-row-col">
          <el-input v-model="searchNameGoods" placeholder="请输入名称" size="small" style="max-width:240px" @keydown.enter="searchGoods">
            <el-button slot="suffix" type="text" size="small" class="search-button" @click="searchGoods">
              <i class="el-icon-search"></i>
            </el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <div>
        <el-table :data="notTuanGoodsList" style="width:100%" stripe>
          <el-table-column prop="id" label="商品ID" align="center"></el-table-column>
          <el-table-column label="商品图片" align="center">
            <template slot-scope="scope">
              <l-pic
                :picurl="scope.row.pic_urls.split(',')[0]"
                :pickey="scope.row.id"
                :picdata="scope.row"
                :picmargin="'0'"
                :size="{ width:30, height:30 }"
                :ischecked="false"
                :disdel="false"/>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="商品名称" align="center"></el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button class="action-button" type="text"
                         @click="choice(scope.row)">选择</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getTuanGoodsList, getNotTuanGoodsList, putTuanGoods } from '@/api/marketing'
import LPic from '@/components/lPic'

export default {
  name: 'setting-yly',
  components: {
    LPic
  },
  data() {
    return {
      page: 1,
      count: 1,
      goodsList: [],
      notTuanGoodsList: [],
      searchName: '',
      searchNameGoods: '',
      openAdd: false,
      title: '选择商品'
    }
  },
  mounted() {
    this.getTuanGoodsList();
  },
  methods: {
    /**
     * 获取拼团商品列表
     */
    getTuanGoodsList() {
      const params = {
        key: this.$store.state.app.activeApp.saa_key,
        page: this.page,
        limit: 10
      };
      getTuanGoodsList(params).then(res => {
        if(res.status === 200){
          this.goodsList = res.data;
          this.count = parseInt(res.count);
        }else if(res.status === 204){
          this.goodsList = [];
          this.count = 1;
        }else{
          this.$message.error(res.message);
        }
      })
    },
    /**
     * 修改状态
     */
    changeStatus(data) {
      const params = {
        key: this.$store.state.app.activeApp.saa_key,
        is_open_assemble: data.is_open_assemble,
        assemble_id: data.id,//拼团id
        id: data.goods_id,//商品ID
      };
      putTuanGoods(params).then(res => {
        if(res.status === 200){
          this.$message.success('状态修改成功');
          this.getTuanGoodsList();
        }else{
          this.$message.error(res.message);
        }
      })
    },
    /**
     * 新建
     */
    add() {
      const params = {
        key: this.$store.state.app.activeApp.saa_key,
      };
      getNotTuanGoodsList(params).then(res => {
        if(res.status === 200){
          this.notTuanGoodsList = res.data;
        }else if(res.status === 204){
          this.notTuanGoodsList = [];
        }else{
          this.$message.error(res.message);
        }
      })
      this.openAdd = true;
    },
    /**
     * 选择一个商品
     */
    choice(data) {
      this.$emit('jump_to_4', {index: 4, data: data, id: '0'});
      this.openAdd = false;
    },
    /**
     * 编辑
     */
    edit(data) {
      this.$emit('jump_to_4', {index: 4, data: data, id: data.id});
    },
    /**
     * 翻页
     */
    changePage(page) {
      this.page = page;
      this.getTuanGoodsList();
    },
    /**
     * 搜索
     */
    search() {
      const params = {
        key: this.$store.state.app.activeApp.saa_key,
        page: this.page,
        limit: 10,
        searchName: this.searchName
      };
      getTuanGoodsList(params).then(res => {
        if(res.status === 200){
          this.goodsList = res.data;
          this.count = parseInt(res.count);
        }else if(res.status === 204){
          this.goodsList = [];
          this.count = 1;
        }else{
          this.$message.error(res.message);
        }
      })
    },
    /**
     * 未拼团商品列表搜索
     */
    searchGoods() {
      const params = {
        key: this.$store.state.app.activeApp.saa_key,
        searchName: this.searchNameGoods
      };
      getNotTuanGoodsList(params).then(res => {
        if(res.status === 200){
          this.notTuanGoodsList = res.data;
        }else if(res.status === 204){
          this.notTuanGoodsList = [];
        }else{
          this.$message.error(res.message);
        }
      })
    }
  },
}
</script>

<style lang="scss" scoped>
.setting-yly-row{
  margin: 15px 0;
  text-align: center;
  height: 40px;
  line-height: 40px;
  &-col{
    padding: 0 15px;
    max-width: 95%;
    overflow: hidden;
    display: flex;
  }
}

</style>